package agendaInterfaz;

/**
 * It is responsible for implementing methods of Agenda with DOM.
 * @author <A HREF="fernandezlopez.raul@gmail.com">Raul Fernandez Lopez</A> 
 * @version 1.00
 */


import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.JMenuBar;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.CardLayout;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.Color;

import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.KeyStroke;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JList;
import javax.swing.border.BevelBorder;
import javax.swing.AbstractListModel;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;
import javax.swing.JComboBox;

import agendaDOM_JAXB.Agenda;
import agendaDOM_JAXB.AgendaDOM;
import agendaDOM_JAXB.AgendaJAXB;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@SuppressWarnings("serial")
public class MainWindow extends JFrame {

	/**
	 * Attribute of contentPane.
	 */
	private JPanel contentPane;
	
	/**
	 * Attribute of frame.
	 */
	private JFrame jframe;
	
	/**
	 * Attribute of main window.
	 */
	private static MainWindow frame;
	
	/**
	 * Set of Agenda.
	 */
	private Agenda agenda;
	
	/**
	 * Attribute of list of contacts.
	 */
	private JList<String> listContactos ;
	/**
	 * Attribute to details of list of contacts.
	 */
	private JList<String> lstDetallesContacto;
	
	/**
	 * Attribute of txtField to nombre.
	 */
	private JTextField txtNombre;
	/**
	 * Attribute of txtField to apellido.
	 */
	private JTextField txtApellido;
	/**
	 * Attribute of txtField to telefono.
	 */
	private JTextField txtTelefono;
	/**
	 * Attribute of txtField to mail.
	 */
	private JTextField txtMail;
	/**
	 * Attribute of txtField to contacto.
	 */
	private JTextField txtIdContacto;
	/**
	 * Attribute of txtField to buscar nombre.
	 */
	private JTextField txtBuscarNombre;
	/**
	 * Attribute of txtField to buscar trabajo.
	 */
	private JTextField txtBuscarTrabajo;
	/**
	 * Attribute of txtField to buscar codigo.
	 */
	private JTextField txtBuscarCod;
	/**
	 * Attribute of button to guardar.
	 */
	private JButton btnGuardar;
	/**
	 * Attribute of comboBox to nombre.
	 */
	private JComboBox<String> cboContactos;
	
	/**
	 * Attribute to handler action .
	 */
	private final Action acCargarContact = new SwingAction();
	/**
	 * Attribute to handler action .
	 */
	private final Action acGuardarContact = new SwingAction_1();
	/**
	 * Attribute to handler action .
	 */
	private final Action acNuevo = new SwingAction_2();
	/**
	 * Attribute to handler action .
	 */
	private final Action acGuardar = new SwingAction_3();
	/**
	 * Attribute to handler action .
	 */
	private final Action acEliminar = new SwingAction_4(); 
	/**
	 * Attribute to handler action .
	 */
	private final Action acFiltrar = new SwingAction_5();
	/**
	 * Attribute to handler action .
	 */
	private final Action acDetalles = new SwingAction_6();
		
	/**
	 * Flag that activates when mousepointe makes click above the button "nuevo".
	 */
    private boolean flagNuevo = false;
   
    /**
     * Method that returns the frame.
     * @return frame of main window
     */
	public MainWindow getFrame() {
		return frame;
	}
	
	/**
	 * Metohd that returns combo box.
	 * @return combo of getCboContactos
	 */
	public JComboBox<String> getCboContactos() {
		return cboContactos;
	}
	
	/**
     * Method that returns list of contacts.
     * @return list of contacts
     */
	public JList<String> getListContactos() {
		return listContactos;
	}
	
	/**
     * Method that returns the textBox of nombre.
     * @return txtnombre
     */
	public JTextField getTxtNombre() {
		return txtNombre;
	}
	
	/**
     * Method that returns the textBox of apellido.
     * @return txtapellido
     */
	public JTextField getTxtApellido() {
		return txtApellido;
	}

	/**
     * Method that returns the textBox of telefono.
     * @return txtTelefono
     */
	public JTextField getTxtTelefono() {
		return txtTelefono;
	}

	/**
     * Method that returns the textBox of mail.
     * @return txtMail
     */
	public JTextField getTxtMail() {
		return txtMail;
	}
	
	/**
     * Method that returns the textBox of contacto.
     * @return txtidContacto
     */

	public JTextField getTxtIdContacto() {
		return txtIdContacto;
	}
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					frame = new MainWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainWindow() {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new CardLayout(0, 0));
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		contentPane.add(tabbedPane);
		
		JPanel panelContactos = new JPanel();
		tabbedPane.addTab("Contactos", null, panelContactos, null);
		tabbedPane.setEnabledAt(0, true);
		
		listContactos = new JList<String>();
		listContactos.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		listContactos.setModel(new AbstractListModel<String>() {
			String[] values = new String[] {};
			public int getSize() {
				return values.length;
			}
			public String getElementAt(int index) {
				return values[index];
			}
		});
		listContactos.setEnabled(true);
		
		JButton btnCargarContactos = new JButton("Cargar Contactos");
		btnCargarContactos.setAction(acCargarContact);
		
		JButton btnGuardarContactos = new JButton("Guardar Contactos");
		btnGuardarContactos.setAction(acGuardarContact);
		GroupLayout gl_panelContactos = new GroupLayout(panelContactos);
		gl_panelContactos.setHorizontalGroup(
			gl_panelContactos.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelContactos.createSequentialGroup()
					.addGroup(gl_panelContactos.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelContactos.createSequentialGroup()
							.addGap(118)
							.addComponent(listContactos, GroupLayout.PREFERRED_SIZE, 428, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_panelContactos.createSequentialGroup()
							.addGap(186)
							.addComponent(btnCargarContactos, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)
							.addGap(60)
							.addComponent(btnGuardarContactos)))
					.addContainerGap(122, Short.MAX_VALUE))
		);
		gl_panelContactos.setVerticalGroup(
			gl_panelContactos.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelContactos.createSequentialGroup()
					.addGap(50)
					.addComponent(listContactos, GroupLayout.PREFERRED_SIZE, 207, GroupLayout.PREFERRED_SIZE)
					.addGap(36)
					.addGroup(gl_panelContactos.createParallelGroup(Alignment.BASELINE)
						.addComponent(btnCargarContactos, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnGuardarContactos, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(76, Short.MAX_VALUE))
		);
		panelContactos.setLayout(gl_panelContactos);
		
		JPanel panelEditar = new JPanel();
		tabbedPane.addTab("Mantenimiento", null, panelEditar, null);
		tabbedPane.setEnabledAt(1, true);
		
		txtNombre = new JTextField();
		txtNombre.setText("");
		txtNombre.setColumns(10);
		
		JLabel lblNombre = new JLabel("Nombre:");
		lblNombre.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		txtApellido = new JTextField();
		txtApellido.setColumns(10);
		
		JLabel lblApellido = new JLabel("Apellidos:");
		lblApellido.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		txtTelefono = new JTextField();
		txtTelefono.setColumns(10);
		
		JLabel lblTelefono = new JLabel("Tel\u00E9fono:");
		lblTelefono.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		txtMail = new JTextField();
		txtMail.setColumns(10);
		
		JLabel lblMail = new JLabel("Correo electr\u00F3nico:");
		lblMail.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		txtIdContacto = new JTextField();
		txtIdContacto.addFocusListener(new TxtIdContactoFocusListener());
		txtIdContacto.setColumns(10);
		
		JLabel lblIDContacto = new JLabel("N\u00BA Contacto:");
		lblIDContacto.setFont(new Font("Tahoma", Font.PLAIN, 13));
		
		JButton btnNuevoContacto = new JButton("Nuevo");
		btnNuevoContacto.setAction(acNuevo);
		
		btnGuardar = new JButton("guardar");
		btnGuardar.setAction(acGuardar);
		btnGuardar.setEnabled(false);
		
		JButton btnEliminar = new JButton("Eliminar");
		btnEliminar.setAction(acEliminar);
		GroupLayout gl_panelEditar = new GroupLayout(panelEditar);
		gl_panelEditar.setHorizontalGroup(
			gl_panelEditar.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panelEditar.createSequentialGroup()
					.addGap(29)
					.addGroup(gl_panelEditar.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panelEditar.createSequentialGroup()
							.addGroup(gl_panelEditar.createParallelGroup(Alignment.TRAILING)
								.addComponent(btnEliminar, GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
								.addComponent(btnNuevoContacto, GroupLayout.PREFERRED_SIZE, 83, GroupLayout.PREFERRED_SIZE))
							.addGap(37)
							.addGroup(gl_panelEditar.createParallelGroup(Alignment.TRAILING)
								.addGroup(gl_panelEditar.createParallelGroup(Alignment.TRAILING)
									.addComponent(lblIDContacto, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 90, GroupLayout.PREFERRED_SIZE)
									.addComponent(lblApellido, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE)
									.addComponent(lblTelefono, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
								.addComponent(lblNombre, GroupLayout.PREFERRED_SIZE, 68, GroupLayout.PREFERRED_SIZE))
							.addGap(34))
						.addGroup(gl_panelEditar.createSequentialGroup()
							.addComponent(lblMail, GroupLayout.PREFERRED_SIZE, 124, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)))
					.addGap(4)
					.addGroup(gl_panelEditar.createParallelGroup(Alignment.LEADING)
						.addComponent(txtIdContacto, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtApellido, GroupLayout.PREFERRED_SIZE, 253, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtTelefono, GroupLayout.PREFERRED_SIZE, 167, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtMail, GroupLayout.PREFERRED_SIZE, 253, GroupLayout.PREFERRED_SIZE)
						.addComponent(txtNombre, GroupLayout.PREFERRED_SIZE, 253, GroupLayout.PREFERRED_SIZE))
					.addContainerGap(140, Short.MAX_VALUE))
				.addGroup(gl_panelEditar.createSequentialGroup()
					.addContainerGap(310, Short.MAX_VALUE)
					.addComponent(btnGuardar)
					.addGap(289))
		);
		gl_panelEditar.setVerticalGroup(
			gl_panelEditar.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelEditar.createSequentialGroup()
					.addGap(34)
					.addGroup(gl_panelEditar.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtIdContacto, GroupLayout.PREFERRED_SIZE, 25, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblIDContacto, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnNuevoContacto))
					.addGap(18, 18, Short.MAX_VALUE)
					.addGroup(gl_panelEditar.createParallelGroup(Alignment.TRAILING)
						.addGroup(gl_panelEditar.createSequentialGroup()
							.addGroup(gl_panelEditar.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtNombre, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNombre, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
							.addGap(34))
						.addGroup(gl_panelEditar.createSequentialGroup()
							.addComponent(btnEliminar)
							.addGap(18)))
					.addGroup(gl_panelEditar.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelEditar.createSequentialGroup()
							.addComponent(txtApellido, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)
							.addGap(31)
							.addGroup(gl_panelEditar.createParallelGroup(Alignment.BASELINE)
								.addComponent(txtTelefono, GroupLayout.PREFERRED_SIZE, 30, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblTelefono, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE)))
						.addComponent(lblApellido, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
					.addGap(26)
					.addGroup(gl_panelEditar.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtMail, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblMail, GroupLayout.PREFERRED_SIZE, 23, GroupLayout.PREFERRED_SIZE))
					.addGap(40)
					.addComponent(btnGuardar)
					.addGap(74))
		);
		panelEditar.setLayout(gl_panelEditar);
		
		JPanel panelBuscar = new JPanel();
		tabbedPane.addTab("Buscar", null, panelBuscar, null);
		
		txtBuscarNombre = new JTextField();
		txtBuscarNombre.setText("");
		txtBuscarNombre.setColumns(10);
		
		txtBuscarTrabajo = new JTextField();
		txtBuscarTrabajo.setColumns(10);
		
		txtBuscarCod = new JTextField();
		txtBuscarCod.setColumns(10);
		
		JLabel lblNombre_1 = new JLabel("Nombre:");
		
		JLabel lblTrabajo = new JLabel("Trabajo:");
		
		JLabel lblCodigoPostal = new JLabel("Codigo Postal:");
		
		JButton btnFiltrar = new JButton("Filtrar");
		btnFiltrar.setAction(acFiltrar);
		
		lstDetallesContacto = new JList<String>();
		lstDetallesContacto.setFont(new Font("Tahoma", Font.PLAIN, 13));
		lstDetallesContacto.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		lstDetallesContacto.setBorder(new LineBorder(new Color(0, 0, 0)));
		
		
		cboContactos = new JComboBox<String>();
		cboContactos.setEnabled(false);
		cboContactos.setEditable(true);
		cboContactos.setMaximumRowCount(15);
		
		JButton btnDetalles = new JButton("Detalles");
		btnDetalles.setAction(acDetalles);
		GroupLayout gl_panelBuscar = new GroupLayout(panelBuscar);
		gl_panelBuscar.setHorizontalGroup(
			gl_panelBuscar.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelBuscar.createSequentialGroup()
					.addGap(151)
					.addGroup(gl_panelBuscar.createParallelGroup(Alignment.TRAILING)
						.addComponent(lblNombre_1)
						.addComponent(lblTrabajo)
						.addComponent(lblCodigoPostal))
					.addGap(43)
					.addGroup(gl_panelBuscar.createParallelGroup(Alignment.LEADING)
						.addComponent(txtBuscarCod, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE)
						.addGroup(gl_panelBuscar.createSequentialGroup()
							.addGroup(gl_panelBuscar.createParallelGroup(Alignment.TRAILING, false)
								.addComponent(txtBuscarTrabajo, Alignment.LEADING)
								.addComponent(txtBuscarNombre, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 178, GroupLayout.PREFERRED_SIZE))
							.addGap(55)
							.addComponent(btnFiltrar, GroupLayout.PREFERRED_SIZE, 81, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(93, Short.MAX_VALUE))
				.addGroup(gl_panelBuscar.createSequentialGroup()
					.addContainerGap(218, Short.MAX_VALUE)
					.addComponent(cboContactos, GroupLayout.PREFERRED_SIZE, 282, GroupLayout.PREFERRED_SIZE)
					.addGap(28)
					.addComponent(btnDetalles)
					.addGap(71))
				.addGroup(gl_panelBuscar.createSequentialGroup()
					.addGap(80)
					.addComponent(lstDetallesContacto, GroupLayout.PREFERRED_SIZE, 533, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(57, Short.MAX_VALUE))
		);
		gl_panelBuscar.setVerticalGroup(
			gl_panelBuscar.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelBuscar.createSequentialGroup()
					.addGap(34)
					.addGroup(gl_panelBuscar.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtBuscarNombre, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblNombre_1))
					.addGap(29)
					.addGroup(gl_panelBuscar.createParallelGroup(Alignment.BASELINE)
						.addComponent(txtBuscarTrabajo, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblTrabajo)
						.addComponent(btnFiltrar))
					.addGap(29)
					.addGroup(gl_panelBuscar.createParallelGroup(Alignment.TRAILING)
						.addComponent(txtBuscarCod, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(lblCodigoPostal))
					.addGap(29)
					.addGroup(gl_panelBuscar.createParallelGroup(Alignment.BASELINE)
						.addComponent(cboContactos, GroupLayout.PREFERRED_SIZE, 22, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnDetalles))
					.addGap(26)
					.addComponent(lstDetallesContacto, GroupLayout.PREFERRED_SIZE, 111, GroupLayout.PREFERRED_SIZE)
					.addContainerGap(64, Short.MAX_VALUE))
		);
		panelBuscar.setLayout(gl_panelBuscar);
		tabbedPane.setEnabledAt(2, true);
		
		setTitle("Aplicaci\u00F3n Agenda");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 691, 499);
		
		JMenuBar menuBarPrincipal = new JMenuBar();
		menuBarPrincipal.setFont(new Font("Tahoma", Font.PLAIN, 14));
		setJMenuBar(menuBarPrincipal);
		
		//Build the first menu.
		JMenu archivo = new JMenu("Archivo");
		archivo.getAccessibleContext().setAccessibleDescription("Opciones de base datos de los contactos");
		menuBarPrincipal.add(archivo);
		//a group of JMenuItems
		JMenuItem menuICargar = new JMenuItem("Cargar contactos...",KeyEvent.VK_C);
		menuICargar.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				VentanaCargar ventanaCargar = new VentanaCargar();
				ventanaCargar.setVisible(true);
			}
		});
		menuICargar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
		archivo.add(menuICargar);
		
		//a group of radio button menu items
		archivo.addSeparator();
		
		//a group of JMenuItems
		JMenuItem menuIGuardar = new JMenuItem("Guardar contactos...",KeyEvent.VK_S);
		menuIGuardar.addMouseListener(new MenuIGuardarMouseListener());
		menuIGuardar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
		archivo.add(menuIGuardar);
		
		//a group of radio button menu items
		archivo.addSeparator();
		
		//a group of JMenuItems
		JMenuItem menuISalir = new JMenuItem("Salir");
		menuISalir.addMouseListener(new MenuISalirMouseListener());
		menuISalir.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));
		archivo.add(menuISalir);
		
		
		//Build the first menu.
		JMenu editar = new JMenu("Editar");
		menuBarPrincipal.add(editar);
		
		//a group of JMenuItems
		JMenuItem menuINuevo = new JMenuItem("Nuevo Contacto",KeyEvent.VK_N);
		menuINuevo.setAction(acNuevo);
		menuICargar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_1, ActionEvent.ALT_MASK));
		editar.add(menuINuevo);
		
		//a group of JMenuItems
		JMenuItem menuIEditar = new JMenuItem("Editar Contacto",KeyEvent.VK_E);
		menuIEditar.setAction(acGuardarContact);
		menuICargar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_2, ActionEvent.ALT_MASK));
		editar.add(menuIEditar);
		
		//a group of JMenuItems
		JMenuItem menuIEliminar = new JMenuItem("Eliminar Contacto",KeyEvent.VK_L);
		menuIEliminar.setAction(acEliminar);
		menuICargar.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_3, ActionEvent.ALT_MASK));
		editar.add(menuIEliminar);
		
		
		//Build the first menu.
		JMenu ver = new JMenu("Ver");
		menuBarPrincipal.add(ver);
		
		
		//a group of JMenuItems
		JMenuItem menuIBuscar = new JMenuItem("Buscar Contacto",KeyEvent.VK_B);
		menuIBuscar.setAction(acFiltrar);
		ver.add(menuIBuscar);
		
		//a group of radio button menu items
		ver.addSeparator();
		
		//a group of JMenuItems
		JMenuItem menuIDetalles = new JMenuItem("Detalles Contacto",KeyEvent.VK_D);
		menuIDetalles.setAction(acDetalles);
		ver.add(menuIDetalles);
	}
	
	
	public class VentanaCargar extends JFrame {

	    /**
		 * 
		 */
		private JPanel cargarContactos;
		private JRadioButton cb1,cb2;
		private JButton b1;
		private ButtonGroup grupo= new ButtonGroup();
		/**
	     * Create the frame.
	     */
	    public VentanaCargar() {
	    	
	        setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
	     // Establecer Gestor de Posicionamiento 
	        
	        
	        cargarContactos = new JPanel();
	        cargarContactos.setBorder(new EmptyBorder(5, 5, 5, 5));
	        setContentPane(cargarContactos);
	        cargarContactos.setLayout(null);
	        setBounds(250,250,300,100);
	        
	        Container cargarContactos = getContentPane(); 
	        cargarContactos.setLayout(new FlowLayout());
	        
	        cb1 = new JRadioButton("DOM");
	        cb1.setMnemonic(KeyEvent.VK_1); 
	        cb1.setSelected(false);
	        
	        cb2 = new JRadioButton("JAXB");
	        cb2.setMnemonic(KeyEvent.VK_2); 
	        cb2.setSelected(false);
	        
	        grupo.add(cb1);
	        grupo.add(cb2);
	      //A�adir al contenedor
	        cargarContactos.add(cb1);
	        cargarContactos.add(cb2);
	        
	        b1 = new JButton("Cargar contactos");
	        b1.setVerticalTextPosition(AbstractButton.BOTTOM);
	        b1.setHorizontalTextPosition(AbstractButton.CENTER); 
	        
	        cargarContactos.add(b1);
	        
	        b1.addMouseListener(new MouseAdapter() {
				@Override
				public void mousePressed(MouseEvent arg0) {
					if (cb1.isSelected()){
						agenda = AgendaDOM.getInstance();
						agenda.loadXML();
						listContactos.setModel(agenda.listContacts());
					}
					if (cb2.isSelected()){
						agenda = AgendaJAXB.getInstance();
						agenda.loadXML();
						listContactos.setModel(agenda.listContacts());
					}
				}
			});
	    }
	    
	}
	
	private class SwingAction extends AbstractAction {
		public SwingAction() {
			putValue(NAME, "Cargar Contactos");
			putValue(SHORT_DESCRIPTION, "Cargar los contactos");
		}
		
		public void actionPerformed(ActionEvent e) {
			
			VentanaCargar ventanaCargar = new VentanaCargar();
			ventanaCargar.setVisible(true);

		}
	}
	private class SwingAction_1 extends AbstractAction {
		public SwingAction_1() {
			putValue(NAME, "Guardar Contacto");
			putValue(SHORT_DESCRIPTION, "Guardar cambios en la lista de contactos");
		}
		public void actionPerformed(ActionEvent e) {
			JOptionPane.showMessageDialog(jframe, agenda.saveXML());
		}
	}
	private class MenuIGuardarMouseListener extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent arg0) {
			JOptionPane.showMessageDialog(jframe, agenda.saveXML());
			
		}
	}
	private class SwingAction_2 extends AbstractAction {
		public SwingAction_2() {
			putValue(NAME, "Nuevo");
			putValue(SHORT_DESCRIPTION, "Crear un contacto nuevo");
		}
		public void actionPerformed(ActionEvent e) {
			String idText = String.valueOf(agenda.getNumContactos()+1);
			txtIdContacto.setText(idText);
			txtNombre.setText("");
			txtApellido.setText("");
			txtTelefono.setText("");
			txtMail.setText("");
			btnGuardar.setEnabled(true);
			
			flagNuevo=true;
		}
	}
	private class SwingAction_3 extends AbstractAction {
		public SwingAction_3() {
			putValue(NAME, "Guardar");
			putValue(SHORT_DESCRIPTION, "Guardar el contacto en la lista de contactos");
		}
		public void actionPerformed(ActionEvent e) {
			try {
				if(!txtIdContacto.getText().isEmpty() || !txtNombre.getText().isEmpty() ||
						!txtApellido.getText().isEmpty() || !txtTelefono.getText().isEmpty() ||
						!txtMail.getText().isEmpty()){
					if (flagNuevo){
						agenda.createNewContact();
						agenda.saveXML();
						JOptionPane.showMessageDialog(jframe, "Se ha creado correctamente");
						txtIdContacto.setText("");
						txtNombre.setText("");
						txtApellido.setText("");
						txtTelefono.setText("");
						txtMail.setText("");
						btnGuardar.setEnabled(false);
						flagNuevo=false;
					}else{
						if (agenda.editContact(Integer.valueOf(txtIdContacto.getText()))){
							JOptionPane.showMessageDialog(jframe, "Se ha modificado y guardado correctamente");
							btnGuardar.setEnabled(false);
						}else{
							JOptionPane.showMessageDialog(jframe, "Haga click primero en: Nuevo");
						}
					}
					
				}else{
					JOptionPane.showMessageDialog(jframe, "Falta alg�n campo por rellenar");
				}
			}catch (IOException e1) {
				JOptionPane.showMessageDialog(jframe, "Error al guardar el contacto");
				e1.printStackTrace();
			}
		}
	}
	
	private class TxtIdContactoFocusListener extends FocusAdapter {
		@Override
		public void focusLost(FocusEvent arg0) {
						
			agenda.searchContactLostfocus(Integer.valueOf(txtIdContacto.getText()));
			btnGuardar.setEnabled(true);
		}
	}
	private class SwingAction_4 extends AbstractAction {
		public SwingAction_4() {
			putValue(NAME, "Eliminar");
			putValue(SHORT_DESCRIPTION, "Elimina contacto");
		}
		public void actionPerformed(ActionEvent e) {
			
			try {
				agenda.deleteContact(Integer.valueOf(txtIdContacto.getText()));
				JOptionPane.showMessageDialog(jframe, "El contacto se ha eliminado correctamente");
			} catch (NumberFormatException e1) {
				JOptionPane.showMessageDialog(jframe, "Error: no se ha encontrado el contacto");
				e1.printStackTrace();
			} catch (IOException e1) {
				JOptionPane.showMessageDialog(jframe, "Error al eliminar el contacto");
				e1.printStackTrace();
			}
			
		}
	}
	private class SwingAction_5 extends AbstractAction {
		public SwingAction_5() {
			putValue(NAME, "Filtrar");
			putValue(SHORT_DESCRIPTION, "Rellena el desplegable con el filtro");
		}
		public void actionPerformed(ActionEvent e) {
			boolean encontrado = true;
			List<String> arrayContactos = new ArrayList<String>();
			if(!txtBuscarNombre.getText().isEmpty()){
				arrayContactos.addAll(agenda.searchByName(txtBuscarNombre.getText()));
			}else if(!txtBuscarTrabajo.getText().isEmpty()){
				arrayContactos.addAll(agenda.searchByJob(txtBuscarTrabajo.getText()));
			}else if (!txtBuscarCod.getText().isEmpty()){
				arrayContactos.addAll(agenda.searchByPostalCode(txtBuscarCod.getText()));
			}else{
				JOptionPane.showMessageDialog(jframe, "No hay ning�n criterio para buscar");
				encontrado=false;
			}
			if (encontrado){
				cboContactos.removeAllItems();
				cboContactos.setEnabled(true);
				cboContactos.setEditable(true);			
				for(int i = 0; i<arrayContactos.size();i++){
					cboContactos.addItem(arrayContactos.get(i));
				}
				cboContactos.setEditable(false);
			}
		}
	}
	private class SwingAction_6 extends AbstractAction {
		public SwingAction_6() {
			putValue(NAME, "Detalles");
			putValue(SHORT_DESCRIPTION, "Descripci�n detallada del contacto");
		}
		public void actionPerformed(ActionEvent e) {
			DefaultListModel<String> lista= new DefaultListModel<String>();
			try {
				lista.addElement(agenda.printContactDetail(Integer.valueOf(cboContactos.getSelectedItem().toString().substring(0,1))));
				lstDetallesContacto.setModel(lista);
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}
	private class MenuISalirMouseListener extends MouseAdapter {
		@Override
		public void mousePressed(MouseEvent arg0) {
			System.exit(EXIT_ON_CLOSE);
		}
	}
}
